package com.myproject.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Druid1 {

    public static DruidPooledConnection conn;

    static {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/giftcptdb?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
        //dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/giftmalldb?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
        //dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/giftmgmdb?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("root123");

        //连接池配置
        dataSource.setMaxActive(200);
        dataSource.setMinIdle(10);
        dataSource.setInitialSize(10);
        dataSource.setMaxWait(60000);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(30000);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setValidationQuery("SELECT 1");

        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {


        try {
         PreparedStatement pst = conn.prepareStatement("select * from tbl_qd_act_cfg");
         ResultSet rs = pst.executeQuery();
             while (rs.next()) {
                System.out.println(rs.getString("act_no") + ":" + rs.getString("act_at"));
             }
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
